method URL.createObjectURL
#URL.createObjectURL(blob: Blob): string
Creates a unique, temporary URL that represents a given Blob, File, or MediaSource object.
This method is particularly useful for:
- Creating URLs for dynamically generated content
- Working with blobs in a browser context
- Creating workers from dynamically generated code
- Setting up temporary URL references for file downloads
Note: Always call URL.revokeObjectURL() when you're done using the URL to prevent memory leaks.
Examples #
#
// Create a URL string for a Blob
const blob = new Blob(["Hello, world!"], { type: "text/plain" });
const url = URL.createObjectURL(blob);
console.log(url); // Logs something like "blob:null/1234-5678-9101-1121"
// Dynamic web worker creation in Deno
const workerCode = `
self.onmessage = (e) => {
self.postMessage(e.data.toUpperCase());
};
`;
const workerBlob = new Blob([workerCode], { type: "application/javascript" });
const workerUrl = URL.createObjectURL(workerBlob);
const worker = new Worker(workerUrl, { type: "module" });
worker.onmessage = (e) => console.log(e.data);
worker.postMessage("hello from deno");
// Always revoke when done to prevent memory leaks
URL.revokeObjectURL(workerUrl);
Parameters #
Return Type #
string